//
//  ExperienceDetailViewController.m
//  MemoryTravel
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 fcx. All rights reserved.
//

#import "ExperienceDetailViewController.h"

@interface ExperienceDetailViewController()

@property (nonatomic, strong)UIWebView *myWebView;
@property (nonatomic, assign)BOOL isCollection;

@property (nonatomic, retain)MBProgressHUD *HUD;

@end

@implementation ExperienceDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"体验详情";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-fanhui.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang.png"] style:UIBarButtonItemStyleDone target:self action:@selector(collectionAction:)];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.myWebView = [UIWebView new];
    self.myWebView.backgroundColor = [UIColor colorWithRed:250 / 255.0 green:245 / 255.0 blue:232 / 255.0 alpha:1.0];
    [self.view addSubview:self.myWebView];
    
    [self.myWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://web.breadtrip.com/hunter/product/%@/", self.product_id];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.myWebView loadRequest:request];
    [self.myWebView resignFirstResponder];
    
    [[DataBaseTools shareDataBase] openDB];
    [[DataBaseTools shareDataBase] createExperienceTable];
    
}

- (void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)collectionAction:(UIBarButtonItem *)barButtonItem{
    if (self.isCollection) {
        self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"shoucang.png"];
        [[DataBaseTools shareDataBase] deleteExperienceWithId:self.product_id];
        
        [self cancelCollection];
    } else {
        self.navigationItem.rightBarButtonItem.image = [[UIImage imageNamed:@"shoucang2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [[DataBaseTools shareDataBase] openDB];
        [[DataBaseTools shareDataBase] createExperienceTable];
        [[DataBaseTools shareDataBase] insertExperienceWithModel:self.model];
        [self successCollection];
    }
    self.isCollection = !self.isCollection;
}

- (void)successCollection{
    self.HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:self.HUD];
    //显示的文字
    self.HUD.labelText = @"收藏成功";
    //是否有遮罩
    self.HUD.dimBackground = NO;
    self.HUD.labelFont = [UIFont boldSystemFontOfSize:22];
    self.HUD.backgroundColor = [UIColor clearColor];
    //提示框的样式
    self.HUD.mode = MBProgressHUDModeCustomView;
    [self.HUD show:YES];
    //两秒之后隐藏
    [self.HUD hide:YES afterDelay:0.5];
}

- (void)cancelCollection{
    self.HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:self.HUD];
    //显示的文字
    self.HUD.labelText = @"取消收藏";
    //是否有遮罩
    self.HUD.dimBackground = NO;
    self.HUD.labelFont = [UIFont boldSystemFontOfSize:22];
    self.HUD.backgroundColor = [UIColor clearColor];
    //提示框的样式
    self.HUD.mode = MBProgressHUDModeCustomView;
    [self.HUD show:YES];
    //两秒之后隐藏
    [self.HUD hide:YES afterDelay:0.5];
}

- (void)viewWillAppear:(BOOL)animated{
    NSMutableArray *experienceCollectionArray = [[DataBaseTools shareDataBase] selectExperience];
    NSMutableArray *tempArray = [NSMutableArray array];
    for (ExperienceModel *model in experienceCollectionArray) {
        [tempArray addObject:model.product_id];
    }
    if ([tempArray containsObject:self.product_id]) {
        self.isCollection = YES;
        self.navigationItem.rightBarButtonItem.image = [[UIImage imageNamed:@"shoucang2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
